import React, { useState, useEffect } from 'react';
import {
  MenuFoldOutlined,
  MenuUnfoldOutlined,
  UploadOutlined,
  UserOutlined,
  VideoCameraOutlined,
} from '@ant-design/icons';
import { Button, Layout, Menu, theme } from 'antd';

import Home from './child/Home';
import ShopList from './child/ShopList';
import UserList from './child/UserList';
import { Switch, Route, Redirect, type RouteComponentProps } from 'react-router-dom';
import { useAppDispatch, useAppSelector } from '../store/reactreduxts';
import { async_getlist } from '../store/crmsSlice';

const { Header, Sider, Content } = Layout;

const Admin: React.FC<RouteComponentProps> = (props) => {

  let dispatch = useAppDispatch()
  //在初始周期中
  useEffect(() => {
    dispatch(async_getlist())
  }, [])

  const [collapsed, setCollapsed] = useState(false);
  const {
    token: { colorBgContainer, borderRadiusLG },
  } = theme.useToken();

  return (
    <Layout style={{ height: "100vh" }}>
      <Sider trigger={null} collapsible collapsed={collapsed}>
        <div className="demo-logo-vertical" />
        <Menu
          theme="dark"
          mode="inline"
          defaultSelectedKeys={[props.location.pathname]} //高亮
          defaultOpenKeys={['', props.location.pathname]} //初始默认展开
          items={[  //配置左侧侧边栏
            {
              key: '/admin/home',
              icon: <UserOutlined />,
              label: '首页',
            },
            {
              key: '',
              icon: <VideoCameraOutlined />,
              label: '系统管理',
              children: [
                {
                  key: '/admin/userlist',
                  icon: <UploadOutlined />,
                  label: '用户管理',
                },
                {
                  key: '/admin/shoplist',
                  icon: <UploadOutlined />,
                  label: '岗位管理',
                },
              ]
            },
          ]}
          onSelect={(e) => {
            //编程式跳转
            props.history.push(e.key)
          }}
        />
      </Sider>
      <Layout>
        <Header style={{ padding: 0, background: colorBgContainer }}>
          <Button
            type="text"
            icon={collapsed ? <MenuUnfoldOutlined /> : <MenuFoldOutlined />}
            onClick={() => setCollapsed(!collapsed)}
            style={{
              fontSize: '16px',
              width: 64,
              height: 64,
            }}
          />
        </Header>
        <Content
          style={{
            margin: '24px 16px',
            padding: 24,
            minHeight: 280,
            background: colorBgContainer,
            borderRadius: borderRadiusLG,
          }}
        >
          {/*  配置子级路由  */}
          <Switch>
            <Route path='/admin/home' component={Home} />
            <Route path='/admin/userlist' component={UserList} />
            <Route path='/admin/shoplist' component={ShopList} />
            <Redirect from='/admin' to='/admin/shoplist' />
          </Switch>
        </Content>
      </Layout>
    </Layout>
  );
};

export default Admin;